<?php
/*------------------------------------------------------------------------
 * 用户管理
 * 	
 * Created on 2015-07-18
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2014 http://www.suncco.com All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace Admin\Controller;
class UserController extends CommonController{
	public $exec_comfunc  = 'index,add,edit';//index,add,edit,delete
	
	public function index(){
		//省份
		$province_id = $this->_get('province_id');
		$info['province_id'] = $province_id;
		if($province_id && !$city_id){
			$where['province_id'] = $province_id;
		}
		//城市
		$city_id = $this->_get('city_id');
		$info['city_id'] = $city_id;
		if($city_id){
			$where['city_id'] = $city_id;
		}
		$this->assign('info',$info);
		//客服城市
		if($this->_city_id){
			$where['city_id'] = array(array('eq',$this->_city_id),array('eq',0),'or');
		}
		// 用户类型
		$user_type = $this->_get('user_type');
		if($user_type){
			$where['is_lawyer'] = $user_type == 'lawyer' ? 1 : 0;
		}
		$this->assign('user_type', $user_type);
		//按姓名搜索
		$nickname = $this->_get('nickname');
		if($nickname){
			$where['nickname'] = array('like', '%'. $nickname . '%');
		}
		$this->assign('nickname', $nickname);

		//按手机号搜索
		$mobile = $this->_get('mobile');
		if($mobile){
			$where['mobile'] = array('like', '%'. $mobile . '%');
		}
		$this->assign('mobile', $mobile);

		// 排序字段
		$order_filed = $this->_get('order_filed');
		if(!empty($order_filed)){
			$order_info = explode('_by_', $order_filed);
			$order[$order_info[0]] = $order_info[1];
		}

		// 排序
		if(empty($order)){
			$order = 'create_time desc';
		}
		
		$list = parent::index('', $where, $order, true);
		foreach ($list as $rs) {
			if($user_type == 'lawyer'){
				$rs['audit_status'] = D('UserLawyer')->where(array('user_id' => $rs['user_id']))->getField('audit_status');
			}
			$new_list[] = $rs;
		}
		unset($where);
		$where['is_lawyer'] = $user_type == 'lawyer' ? 1 : 0;
		$total_num = D('User')->where($where)->count();
		
		$where['create_time'] = array('egt', strtotime(date('Y-m-d')));
		$today_num = D('User')->where($where)->count();
		$this->assign('total_num', $total_num);
		$this->assign('today_num', $today_num);
		$this->assign('list', $new_list);
		$this->display();
	}
	// 设置用户状态
	public function set_status(){
		$user_id = $this->_get('user_id','UserID不能为空！');

		$where['user_id'] = $user_id;
		$status = D('User')->where($where)->getField('status');

		$data['status'] = $status ? 0 : 1;
		D('User')->where($where)->save($data);
		// 禁用时清空TOKEN登录时间
		if($status){
			$where['user_id'] = $user_id;
			D('UserToken')->where($where)->save(array('create_time' => 0));
		}
		$this->success('设置成功！');
	}
	// 设置客服
	public function win_set_service(){
		$user_id = $this->_get('user_id', '对不起，您的操作有误！');
		$where['user_id'] = $user_id;
		$info = D('User')->where($where)->find();
		if(IS_POST){
			$data['service_id'] = $this->_post('service_id');
			D('User')->where($where)->save($data);
			$this->success('设置成功！');
		}

		//客服人员列表
		$admin_list = D('User')->admin_user_list($this->_city_id);
		$this->assign('admin_list', $admin_list);
		$this->assign('info', $info);
		$this->display();
	}
	// 查看用户详情
	public function win_detail(){
		$user_id   = $this->_get('user_id', '对不起，您的操作有误！');
		$user_type = $this->_get('user_type');

		$where['user_id'] = $user_id;
		$info = D('User')->where($where)->find();
		$lawyer = D('UserLawyer')->where($where)->find();

		//审核理由列表
		$where['type']    = 'lawyer_user';
		$where['user_id'] = $user_id;
		$audit_reasons = D('AuditReasons')->where($where)->select();

		$this->assign('info', $info);
		$this->assign('lawyer', $lawyer);
		$this->assign('user_type', $user_type);
		$this->assign('audit_reasons', $audit_reasons);
		$this->display();
	}
	// 审核律师
	public function win_audit_lawyer(){
		$user_id   = $this->_get('user_id', '对不起，您的操作有误！');
		$where['user_id'] = $user_id;
		$info = D('UserLawyer')->where($where)->find();
		if(!$info){
			$this->error("用户信息不存在！");
		}
		if(IS_POST){
			$status  = $this->_post('status');
			$content = $status == 2 ? $this->_post('content', '请填写不通过原因！') : '';

			$data['type']        = 'lawyer_user';
			$data['user_id']     = $user_id;
			$data['type_id']     = $user_id;
			$data['content']     = $content;
			$data['create_time'] = time();
			$data['status']      = $status;
			$data['admin_id']    = $this->_user_id;
			D('AuditReasons')->add($data);
			// 更新审核状态
			D('UserLawyer')->where($where)->save(array('audit_status' => $status));

			$this->success('提交成功！');
		}

		unset($where);
		//审核理由列表
		$where['type']    = 'lawyer_user';
		$where['user_id'] = $user_id;
		$audit_reasons = D('AuditReasons')->where($where)->select();
		
		$this->assign('info', $info);
		$this->assign('audit_reasons', $audit_reasons);
		$this->display();
	}
	// 选择用户
	public function win_select_user(){
		$type = $this->_get('type', '', 'all');
		$skilled_id = $this->_get('skilled_id');
		$info['skilled_id'] = $skilled_id;
		switch ($type) {
			case 'lawyer':
				$where['is_lawyer'] = 1;
				if($skilled_id){
					$user_id_arr = D('UserLawyer')->where(array('skilled_id' => $skilled_id))->getField('user_id', true);
					$user_id_arr = $user_id_arr ? $user_id_arr : 'empty';
				}
				break;

			case 'user':
				$where['is_lawyer'] = 0;
				break;

			default:
				break;
		}
		// 按用户ID搜索
		$user_id = $this->_get('user_id');
		if($user_id){
			$where['user_id'] = $user_id;
		}
		$this->assign('user_id', $user_id);

		if($user_id_arr){
			$where['user_id'] = 1;
			if(is_array($where['user_id'])){
				$user_id_arr[] = $where['user_id'];
				$user_id_str = implode(',', $user_id_arr);
			}
			$where['user_id'] = array('in', $user_id_str ? $user_id_str : '1');
		}
		// 按姓名搜索
		$realname = $this->_get('realname');
		if($realname){
			$where['realname'] = array('like', '%' . $realname . '%');
		}
		$this->assign('realname', $realname);

		// 按手机号搜索
		$mobile = $this->_get('mobile');
		if($mobile){
			$where['mobile'] = $mobile;
		}

		//省份
        $province_id = $this->_get('province_id');
        $info['province_id'] = $province_id;
        if($province_id && !$city_id){
            $where['province_id'] = $province_id;
        }
        //城市
        $city_id = $this->_get('city_id');
        $info['city_id'] = $city_id;
        if($city_id){
            $where['city_id'] = $city_id;
        }
        $this->assign('info', $info);
        //客服城市
        if($this->_city_id){
            $where['city_id'] = $this->_city_id;
        }
  //       var_dump($user_id_arr);
		// var_dump($user_id_str);
		// var_dump($where);
		// exit;
		$list = parent::index('', $where, 'create_time desc', true);
        unset($where);
        foreach ($list as $rs) {
        	if($type == 'lawyer'){
        		$rs['lawyer_is_doing'] = D('Order')->lawyer_is_doing($rs['user_id']);
        	}
            $data[] = $rs;
        }
        $this->assign('list', $data);
        $this->assign('is_mulit', $this->_get('is_mulit', '', '0'));
        $this->assign('user_field', $this->_get('user_field', '', 'user_id'));
        $this->display();
	}
}
